#include "open_piece_dialog.h"

//--------------------------------------------------------------------------------------------
Open_piece_dialog::Open_piece_dialog(QString *library_path, int *piece_index, QWidget *parent) : QDialog(parent)
{
	this->current_library_path = library_path;
	this->current_piece_index = piece_index;
	this->piece_index = -1;
	ui.setupUi(this);
	this->setupConnections();
	this->initialize();
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::accept_()
{
	*this->current_library_path = this->library_path;
	*this->current_piece_index = this->piece_index;
	this->close();
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::cancel_()
{
	this->close();
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::select_library()
{
        this->library_path = QCoreApplication::applicationDirPath() + "/librerias/" + this->ui.cb_libraries->currentText();
	this->current_library = Library(this->library_path);
	Library::LoadPiecesList(this->library_path, this->ui.lw_pieces);
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::select_piece()
{
	this->piece_index = this->ui.lw_pieces->currentIndex().row();
	this->updateAtributtes();
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::initialize()
{
        Library::LoadLibraryList(QCoreApplication::applicationDirPath() + "/librerias", this->ui.cb_libraries);
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::setupConnections()
{
	connect(this->ui.cb_libraries, SIGNAL(currentIndexChanged(QString)), this, SLOT(select_library()));
	connect(this->ui.lw_pieces, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(select_piece()));
	connect(this->ui.lw_pieces, SIGNAL(itemDoubleClicked(QListWidgetItem *)), this, SLOT(accept_()));
	connect(this->ui.pb_open, SIGNAL(clicked()), this, SLOT(accept_()));
	connect(this->ui.pb_cancel, SIGNAL(clicked()), this, SLOT(cancel_()));
}

//--------------------------------------------------------------------------------------------
void Open_piece_dialog::updateAtributtes()
{
	Piece current_piece = this->current_library.getPieceAt(this->piece_index);
	this->ui.lb_nameLib->setText(this->ui.cb_libraries->currentText());
	this->ui.lb_pieceIndex->setText(QVariant((piece_index)).toString());
	this->ui.lb_color->setPalette(QPalette(current_piece.getColor()));
	this->ui.lb_color->setText(current_piece.getColor().name());
	this->ui.lb_measures->setText(QVariant(current_piece.width()).toString() + " x " + QVariant(current_piece.height()).toString());
	this->ui.lb_countPoints->setText("Cantidad de puntos: " + QVariant(current_piece.count()).toString());
}

//--------------------------------------------------------------------------------------------
Open_piece_dialog::~Open_piece_dialog()
{
}
